/*
 * Copyright (c) 2011 Nathan Byrd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.stackconfig.web.gwt.client.widgets;

import com.allen_sauer.gwt.dnd.client.HasDragHandle;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Creates a wrapper widget that can be dragged
 * See: http://allen-sauer.com/com.allen_sauer.gwt.dnd.demo.DragDropDemo/DragDropDemo.html#PaletteExample
 */
public class DraggableWrapperWidget extends AbsolutePanel implements HasDragHandle {
    private FocusPanel shim = new FocusPanel();
    private final Component component;

    public Widget getComponent() {
        return component;
    }

    /**
     * Construct a new DraggableWrapperWidget based on existing widget
     *
     * @param component Widget to wrap
     */
    public DraggableWrapperWidget(Component component) {
        this.component = component;
        add(component);
    }

    /**
     * Clone the widget
     *
     * @return DraggableWrapperWidget clone of existing object
     *         TODO: Handle more than labels
     */
    public DraggableWrapperWidget cloneWidget() {
        Component clone = component.cloneWidget();

        clone.setStyleName(component.asWidget().getStyleName());
        clone.setTitle(component.asWidget().getTitle());
        return new DraggableWrapperWidget(clone);
    }

    /**
     * Return a shim for dragging
     *
     * @return Widget shim
     */
    @Override
    public Widget getDragHandle() {
        return shim;
    }

    /**
     * Let shim size match our size.
     *
     * @param width  the desired pixel width
     * @param height the desired pixel height
     */
    @Override
    public void setPixelSize(int width, int height) {
        super.setPixelSize(width, height);
        // Always set the shim size to the size of the top component
        int topComponentWidth = component.getTopComponent().getOffsetWidth();
        int topComponentHeight = component.getTopComponent().getOffsetHeight();
        shim.setPixelSize(topComponentWidth, topComponentHeight);
    }

    /**
     * Let shim size match our size.
     *
     * @param width  the desired CSS width
     * @param height the desired CSS height
     */
    @Override
    public void setSize(String width, String height) {
        super.setSize(width, height);
        // Always set the shim size to the size of the top component
        int topComponentWidth = component.getTopComponent().getOffsetWidth();
        int topComponentHeight = component.getTopComponent().getOffsetHeight();
        shim.setSize(topComponentWidth + "px", topComponentHeight + "px");
    }

    /**
     * Adjust the shim size and attach once our widget dimensions are known.
     */
    @Override
    protected void onLoad() {
        super.onLoad();

        int width = component.getTopComponent().getOffsetWidth();
        int height = component.getTopComponent().getOffsetHeight();
        shim.setPixelSize(width, height);

        add(shim, 0, 0);
    }

    /**
     * Remove the shim to allow the widget to size itself when reattached.
     */
    @Override
    protected void onUnload() {
        super.onUnload();
        shim.removeFromParent();
    }

}
